﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TrackBar
{
	/// <summary>
	/// Interaction logic for MainControl.xaml
	/// </summary>
	public partial class MainControl
	{
		private int secondsPass;
		public int SecondsPass
        {
            get {return Convert.ToInt32(slider.Value);}
		}
		private int secondsLeft;
		public int SecondsLeft
        {
			get{return secondsCount - Convert.ToInt32(slider.Value);}
		}
		
		private int secondsCount = 0;
		private double trackBarWidth = 200;
		public double TrackBarWidth
		{
			get {return trackBarWidth;}
			set 
			{
				trackBarWidth = value;
				slider.Width = value;
			}
		}
        public int SecondsCount
        {
            get { return secondsCount; }
            set 
            { 
                secondsCount = value;
                slider.Maximum = secondsCount;
                secsYet = secondsCount;
                upTime.Text = "00:00";
                string minutes = "";
                string seconds = "";
                calcTime(ref minutes, ref seconds,secsYet);
                downTime.Text = '-' + minutes + ':' + seconds;
            }
        }

        private int secsLater = 0;
        private int secsYet = 0;
		
		public MainControl()
		{	
			this.InitializeComponent();	
			
		}
		
		 void calcTime(ref string minutes,ref string seconds,int counter)
        {
            int mins = counter / 60;
            int secs = counter - mins * 60;
            minutes = mins.ToString();
            if (minutes.Length == 1)
                minutes = "0" + minutes;
            seconds = secs.ToString();
            if (seconds.Length == 1)
                seconds = "0" + seconds;
        }

        private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            secsLater = Convert.ToInt32(slider.Value);
            secsYet = secondsCount - secsLater;
            string minutes = "";
            string seconds = "";
            calcTime(ref minutes, ref seconds, secsYet);
            downTime.Text = '-' + minutes + ':' + seconds;
            minutes = "";
            seconds = "";
            calcTime(ref minutes, ref seconds, secsLater);
            upTime.Text = minutes + ':' + seconds;
        }

        private void slider_GotMouseCapture(object sender, MouseEventArgs e)
        {
			try
			{
				var dx = e.GetPosition(slider).X;
            	var part = dx / slider.Width;
            	slider.Value = part * slider.Maximum;
			}
			catch
			{}

        }
	}
}